/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Model.Case;
import Model.Grille;
import Model.Jeu;
import Model.Piece;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.*;
import javax.swing.border.Border;

/**
 *
 * @author p1005794
 */
public class Vue extends JFrame implements Observer , KeyListener, ActionListener{

    private Jeu jeu;
    private JPanel jPanelJeu;
    private JPanel jPanelPrediction;
    private JMenuItem facile;
    private JMenuItem inter;
    private JMenuItem difficile;
    private Label score;

    public Vue(Jeu jeu) {
        super();
        this.jeu = jeu;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        build();
        this.addKeyListener(this);
    }

    public final void build() {
        setTitle("Tetris");
        setSize(500, 550);

        //MENU
        JMenuBar menu = new JMenuBar();
        JMenu m = new JMenu("Difficulté");
        facile = new JMenuItem("Facile");
        inter = new JMenuItem("Intermédiaire");
        difficile = new JMenuItem("Hardcore");
        m.add(facile);
        m.add(inter);
        m.add(difficile);
        menu.add(m);
        facile.addActionListener(this);
        inter.addActionListener(this);
        difficile.addActionListener(this);
        
        setJMenuBar(menu);

        //JPANELS
        JComponent jPanelPrincipal = new JPanel(new BorderLayout());
        jPanelJeu = new JPanel(new GridLayout(20, 10));
        Border blackline = BorderFactory.createLineBorder(Color.black, 1);
        jPanelJeu.setBorder(blackline);

        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 10; j++) {
                JComponent jCase = new JPanel();
                jCase.setBorder(blackline);
                jPanelJeu.add(jCase);
            }
        }
        
                
                
        //Suite JPANELS
        JComponent jPanelBlocs = new JPanel(new BorderLayout());
        JComponent jPanelRemplissage= new JPanel(new FlowLayout());
        JComponent jPanelRemplissage2 = new JPanel();
        jPanelPrediction = new JPanel(new GridLayout(4,4));
        
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                JComponent jCase = new JPanel();
                jPanelPrediction.add(jCase);
            }
        }
        
        Label label = new Label("---- Pièce suivante ----");
        score = new Label("Score : " + jeu.getScore());
        jPanelRemplissage.add(score);
        jPanelRemplissage2.add(label);
        
        jPanelJeu.setPreferredSize(new Dimension(275, 0));
        jPanelPrediction.setPreferredSize(new Dimension(120,120));
  
        jPanelRemplissage2.add("Center", jPanelPrediction);
        jPanelBlocs.add("North", jPanelRemplissage2);
        jPanelBlocs.add("South", jPanelRemplissage);
        jPanelPrincipal.add("West", jPanelJeu);
        jPanelPrincipal.add("Center", jPanelBlocs);
        jPanelPrincipal.add("East", new JPanel());
        add(jPanelPrincipal);
    }

    @Override
 public void update(Observable o, Object o1) {
        Border blackline = BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1, rootPaneCheckingEnabled);
        if(jeu.getGameOver()){
            Grille grille = jeu.getGrille();
            int[][] tabOver = Piece.pieceOver;
            //Mise en place des cases dans JpanelJeu
            
            jPanelJeu.setBorder(blackline);
            
            int tmp_i = 0;
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 10; j++) {
                    if (i > 0) {
                        tmp_i = i * 10 + j;//Permet le parcous en liste de JPanelJeu
                    } else {
                        tmp_i = i + j;
                    }
                    if(tabOver[i][j] == 1){
                        JComponent jCase = (JComponent) jPanelJeu.getComponent(tmp_i);
                        jCase.setBackground(Color.DARK_GRAY);
                        jCase.setBorder(blackline); 
                    }
                    else{
                        JComponent jCase = (JComponent) jPanelJeu.getComponent(tmp_i);
                        jCase.setBackground(Color.WHITE);
                        jCase.setBorder(blackline);                  
                    }
                }
            }
            int retour = JOptionPane.showConfirmDialog(this, "GAME OVER", "Partie terminée", JOptionPane.CLOSED_OPTION);
            
            if(retour == JOptionPane.OK_OPTION){
                System.exit(0);
            }
        }else{
        
            Grille grille = jeu.getGrille();

            //Mise en place des cases dans JpanelJeu
            jPanelJeu.setBorder(blackline);
            score.setText("Score : "+ jeu.getScore());

            int tmp_i = 0;
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 10; j++) {
                    if (i > 0) {
                        tmp_i = i * 10 + j;//Permet le parcous en liste de JPanelJeu
                    } else {
                        tmp_i = i + j;
                    }
                    JComponent jCase = (JComponent) jPanelJeu.getComponent(tmp_i);
                    jCase.setBorder(null);
                    jCase.setBackground(Color.white);
                    if(grille.getMatrice()[i][j].getOccupe()){
                        jCase.setBorder(blackline);
                        jCase.setBackground(grille.getMatrice()[i][j].getColor());
                    }                   

                }
            }
            tmp_i = 0;
            Case[][] tabPieceSuivante = jeu.getPieceSuivante().getTabPiece();
            for(int i = 0; i< 4; i++){
                for(int j = 0; j<4; j++){
                    JComponent jCase = (JComponent) jPanelPrediction.getComponent(tmp_i);
                    jCase.setBackground(null);
                    jCase.setBorder(null);
                    
                    if(tabPieceSuivante[i][j].getOccupe()){
                        jCase.setBackground(tabPieceSuivante[i][j].getColor());
                        jCase.setBorder(blackline);
                    }                    
                    tmp_i++;
                }
            }
        }
    }
    
    @Override
    public void keyTyped(KeyEvent e) {       
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_UP){
            jeu.rotationGauche();          
        }
        if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            jeu.translationDroite();          
        }
        if(e.getKeyCode() == KeyEvent.VK_LEFT){
            jeu.translationGauche();                          
        }
        if(e.getKeyCode() == KeyEvent.VK_SPACE){
            jeu.descenteToutbas(); 
        }
        if(e.getKeyCode() == KeyEvent.VK_DOWN){
            jeu.translationBas();
        }
        if(e.getKeyCode() == KeyEvent.VK_S){
            jeu.swap();
        } 
        
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if(source == facile){
            jeu.setLevel(400);
        }
        if(source == inter){
            jeu.setLevel(200);
        }
         if(source == difficile){
            jeu.setLevel(100);
        }
    
    }
}
